package im.starchat.chat.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import im.starchat.chat.R;

import java.util.HashMap;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class MainTabLiveFragment extends Fragment implements OnClickListener {

    @InjectView(R.id.feed_zhibo)
    TextView mZhiboText;
    @InjectView(R.id.feed_notice)
    TextView mNoticeText;
    @InjectView(R.id.feed_main)
    FrameLayout mContainer;

    private int mCurrentTabID;

    private Map<Integer, Fragment> fragmentMap = new HashMap<>();

    public static MainTabLiveFragment newInstance() {
        return new MainTabLiveFragment();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_feed, container, false);
        ButterKnife.inject(this, view);

        LiveContainerFragment liveContainerFragment = LiveContainerFragment.newInstance();
        PreviewFragment paradeFragment = PreviewFragment.newInstance();
        fragmentMap.put(R.id.feed_zhibo, liveContainerFragment);
        fragmentMap.put(R.id.feed_notice, paradeFragment);
        mZhiboText.setBackgroundResource(R.drawable.bg_rounded_feed_tab);
        mZhiboText.setTextColor(getResources().getColor(R.color.color_black));
        mZhiboText.setOnClickListener(this);
        mNoticeText.setOnClickListener(this);

        FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.feed_main, liveContainerFragment);
        fragmentTransaction.commit();

        return view;
    }

    public Fragment getCurrentFragment() {
        return fragmentMap.get(mCurrentTabID);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.feed_zhibo:
                changeTab(true);
                break;
            case R.id.feed_notice:
                changeTab(false);
                break;
        }
    }

    private void changeTab(boolean isZhibo){
        if(isZhibo){
            mCurrentTabID = R.id.feed_zhibo;
            mZhiboText.setBackgroundResource(R.drawable.bg_rounded_feed_tab);
            mZhiboText.setTextColor(getResources().getColor(R.color.color_black));
            mNoticeText.setBackground(null);
            mNoticeText.setTextColor(getResources().getColor(R.color.color_text_5));
        }else{
            mCurrentTabID = R.id.feed_notice;
            mNoticeText.setBackgroundResource(R.drawable.bg_rounded_feed_tab);
            mNoticeText.setTextColor(getResources().getColor(R.color.color_black));
            mZhiboText.setBackground(null);
            mZhiboText.setTextColor(getResources().getColor(R.color.color_text_5));
        }

        FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
        getCurrentFragment().onPause();
        Fragment fragment = fragmentMap.get(mCurrentTabID);
        if (fragment.isAdded()) {
            fragment.onResume();
        } else {
            fragmentTransaction.add(R.id.feed_main, fragment);
        }
        for (Map.Entry entry : fragmentMap.entrySet()) {
            Fragment iteratorFragment = (Fragment) entry.getValue();
            if (entry.getKey().equals(mCurrentTabID)) {
                fragmentTransaction.show(iteratorFragment);
            } else {
                fragmentTransaction.hide(iteratorFragment);
            }
        }
        fragmentTransaction.commit();
    }


}
